package com.taobao.com;

import java.io.IOException;
import java.net.URLDecoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import sun.misc.BASE64Encoder;
//import com.taobao.api.internal.util.codec.Base64;
import org.apache.commons.codec.binary.Base64; 
import org.apache.commons.lang3.StringUtils;

 




public class Util {
	 public final static String APP_KEY = "12647603"; //申请应用的appkey
     public final static String APP_SERCET = "f2493ee76a1c2dff98e4c4e7006d5c99";//申请应用的appsecret 
     public final static String TEST_URI="http://gw.api.taobao.com/router/rest";//接口访问地址
     public final static String REDIRECT_URI="http://eagle.gotoip3.com:8080/Test_verify/Verfy_Sign";//请求登录后的回调页面
	/** 
     * 验证TOP回调地址的签名是否合法。要求所有参数均为已URL反编码的。 
     * @param topParams TOP私有参数（未经BASE64解密） 
     * @param topSession TOP私有会话码 
     * @param topSign TOP回调签名 
     * @param appKey 应用公钥                                             
     * @param appSecret 应用密钥 
     * @param appSecret 应用密钥 
     * @return 验证成功返回true，否则返回false 
     * @throws NoSuchAlgorithmException 
     * @throws IOException 
     */ 
     public static boolean verifyTopResponse(String topParams, String topSession, String topSign, 
         String appKey, String appSecret) throws NoSuchAlgorithmException, IOException { 
         StringBuilder result = new StringBuilder(); 
         MessageDigest md5 = MessageDigest.getInstance("MD5"); 
         result.append(appKey).append(topParams).append(topSession).append(appSecret); 
         byte[] bytes = md5.digest(result.toString().getBytes("UTF-8")); 
         BASE64Encoder encoder = new BASE64Encoder(); 
         return encoder.encode(bytes).equals(topSign); 
   }
     /**
      * 
      * @param top_parameters
      * @return
      */
     public static String  ParametersName(String top_parameters){ 
         String nick=null; 
         
         Map<String, String> map= convertBase64StringtoMap(top_parameters); 
         Iterator keyValuePairs = map.entrySet().iterator(); 
         for (int i = 0; i < map.size(); i++){ 
             Map.Entry entry = (Map.Entry) keyValuePairs.next(); 
             Object key = entry.getKey(); 
             Object value = entry.getValue(); 
             if(key.equals("visitor_nick")){ 
                 nick=(String) value; 
               } 
             } 
         return nick;
        
 	
 	}
     /**
      * 
      * @param str
      * @return
      */
     public static Map convertBase64StringtoMap(String str) { 
     if (str == null) 
          return null; 
      String keyvalues = null; 
      try { 
    	  //此处还可以导入SDK中的com.taobao.api.internal.util.codec.Base64
    	  keyvalues = new String(Base64.decodeBase64(URLDecoder.decode(str, "utf-8").getBytes("utf-8"))); 
      } catch (Exception e) { 
          e.printStackTrace(); 
      } 
      String[] keyvalueArray = keyvalues.split("\\&"); 
      Map<String, String> map = new HashMap<String, String>(); 
      for (String keyvalue : keyvalueArray) { 
          String[] s = keyvalue.split("\\="); 
          if (s == null || s.length != 2) 
              return null; 
          map.put(s[0], s[1]); 
      } 
          return map; 
  } 
//     public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
//    	System.out.println(ParametersName("aWZyYW1lPTEmdHM9MTMxODQ5MzYxNTQzNSZ2aXNpdG9yX2lkPTI2MzY2NDIyMSZ2aXNpdG9yX25pY2s95ZWG5a625rWL6K+V5biQ5Y+3MTc=")); 
//    	System.out.println(verifyTopResponse(URLDecoder.decode("aWZyYW1lPTEmdHM9MTMxOTE3NzMwNjE3MyZ2aXNpdG9yX2lkPTY2OTM5MDYxJnZpc2l0b3Jfbmljaz3O6bn6z9w%3D", "utf-8"),"41021237038f370b7131f0228c47bdqAtTgQ6D2f959fc1a669390611",URLDecoder.decode("dpO71DAVo0EuoHFUROd%2F1w%3D%3D", "utf-8"),"12394443","508e3a5c444a378c47756c62f4325de1"));
//     }
    

   
}
